package presale_house.show.view;

import presale_house.entity.ClientDo;
import presale_house.entity.HouseDo;
import presale_house.handler.AddHouseViewHandler;
import presale_house.handler.UpdateHouseViewHandler;
import presale_house.service.ClientService;
import presale_house.service.HouseService;
import presale_house.service.impl.ClientServiceImpl;
import presale_house.service.impl.HouseServiceImpl;
import presale_house.show.HouseList.HouseView;

import javax.swing.*;
import java.awt.*;
import java.util.Vector;

public class UpdateHouseView extends JDialog {
    JPanel jPanel=new JPanel(new FlowLayout(FlowLayout.CENTER,10,20));
    JLabel idLabel=new JLabel("编号",JLabel.RIGHT);
    JTextField idText=new JTextField();
    JLabel houseLayoutLabel=new JLabel("户型",JLabel.RIGHT);
    JComboBox houseLayout;
    JLabel areaLabel=new JLabel("面积",JLabel.RIGHT);
    JTextField areaText=new JTextField();
    JLabel featuresLabel=new JLabel("特点",JLabel.RIGHT);
    JTextField featuresText=new JTextField();
    JLabel stateLabel=new JLabel("售出情况",JLabel.RIGHT);
    JComboBox state;
    JButton addBtn=new JButton("修改");

    HouseDo houseDo =new HouseDo();
//    AddClientViewHandler addClientViewHandler;
    UpdateHouseViewHandler updateHouseViewHandler;




    public UpdateHouseView(HouseView houseView,int selectedHouseId){
        super(houseView,"添加客户",true);
        updateHouseViewHandler =new UpdateHouseViewHandler(this,houseView);
        //回显数据
        HouseService houseService =new HouseServiceImpl();
        HouseDo selectedHouse= houseService.getById(selectedHouseId);
        //添加组件
        idLabel.setPreferredSize(new Dimension(80,30));
        idText.setPreferredSize(new Dimension(200,30));
        idText.setText(selectedHouse.getId()+"");
        //设置Id不可编辑
        idText.setEnabled(false);
        jPanel.add(idLabel);
        jPanel.add(idText);

        //        nameText.setText(selectedClient.getName());
        Vector<String>houseLayoutData=houseDo.getHouseLayouts();
        houseLayout=new JComboBox(houseLayoutData);

        houseLayoutLabel.setPreferredSize(new Dimension(80,30));
        houseLayout.setPreferredSize(new Dimension(200,30));
        //houseLayoutText.setText(selectedHouse.getHouse_layout().toString());
        jPanel.add(houseLayoutLabel);
        jPanel.add( houseLayout);


        areaLabel.setPreferredSize(new Dimension(80,30));
        areaText.setPreferredSize(new Dimension(200,30));
        areaText.setText(selectedHouse.getArea());
        jPanel.add( areaLabel);
        jPanel.add (areaText);

        featuresLabel.setPreferredSize(new Dimension(80,30));
        featuresText.setPreferredSize(new Dimension(200,30));
        featuresText.setText(selectedHouse.getFeatures());
        jPanel.add( featuresLabel);
        jPanel.add(featuresText);


        //
        Vector<String>stateData=houseDo.getStates();
        state=new JComboBox(stateData);
        stateLabel.setPreferredSize(new Dimension(80,30));
        state.setPreferredSize(new Dimension(200,30));
      //  stateText.setText(selectedHouse.getState().toString());
        jPanel.add( stateLabel);
        jPanel.add(state);



        addBtn.addActionListener(updateHouseViewHandler);
        jPanel.add(addBtn);
        //
        Container container=getContentPane();
        container.add(jPanel);


        setSize(350,500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setResizable(false);
        setVisible(true);
    }


    //设置一个客户信息
    public HouseDo buildUpdateHouse(){
        HouseDo houseDo =new HouseDo();
        houseDo.setId(Integer.parseInt(idText.getText()));
        houseDo.setHouse_layout( houseLayout.getSelectedItem().toString());
        houseDo.setArea(  areaText.getText());
        houseDo.setFeatures(featuresText.getText());
        houseDo.setState(state.getSelectedItem().toString());
        return houseDo;
    }
}
